<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;


class DtkModel extends Model
{
    protected $table = 'think_dataoke_product';
    protected $primaryKey = "ID";
    protected $fillable = ['ID','GoodsID','Title','D_title','Pic','Cid','Org_Price','Price','IsTmall','Sales_num','Dsr','SellerID','Yongjin_type','Commission','Commission_jihua','Commission_queqiao','Jihua_link','Que_siteid','Jihua_shenhe','Introduce','Quan_id','Quan_price','Quan_time','Quan_surplus','Quan_receive','Quan_condition','Quan_m_link','Quan_link','Ctime'];
    public $timestamps = false;//关闭laravel自带更新created_at,updated_at,deleted_at的操作

    public function addData($data)
    {//批量添加数据
        //insert批量插入数组
        $res = self::insert($data);
        if($res)
        {
            return $res;
        }
        return false;

    }
    public function addOneData($data)
    {//更新数据
        //create插入一条数据

        $res = self::create($data);
        //$da=["ID"=>1,"GoodsID"=>"GoodsID","Title"=>"Title","D_title"=>"D_title","Pic"=>"14","Cid"=>1,"Org_Price"=>12,"Price"=>12,"IsTmall"=>1,"Sales_num"=>2,"Dsr"=>"Dsr","SellerID"=>"234234","Yongjin_type"=>"3","Commission"=>1,"Commission_jihua"=>2.3,"Commission_queqiao"=>1.2,"Jihua_link"=>"Jihua_link","Que_siteid"=>1,"Jihua_shenhe"=>3,"Introduce"=>"Introduce","Quan_id"=>"Quan_id","Quan_price"=>25,"Quan_time"=>"2017-10-14 23:59:59","Quan_surplus"=>1,"Quan_receive"=>1,"Quan_condition"=>"Quan_condition","Quan_m_link"=>"Quan_m_link","Quan_link"=>"Quan_link","Ctime"=>"2017-10-14 23:59:59"];
        // $res = self::create($da)->toSql();
        if($res)
        {
            return $res;
        }
        return false;
    }
    public function updateData($data)
    {//可以批量更新数据

        //$dtkData = self::find($data['ID']);
        //$res = $dtkData->update($data);

        //使用了更加便捷的更新
        $res = self::where(['ID'=>$data['ID']])->update($data);
        if($res)
        {
            return $res;
        }
        return false;
    }

    public function listData()
    {

        $select = 'ID,GoodsID,Title,D_title,Pic,Cid,Org_Price,Price,IsTmall,Sales_num,Dsr,SellerID,Yongjin_type,Commission,Commission_jihua,Commission_queqiao,Jihua_link,Que_siteid,Jihua_shenhe,Introduce,Quan_id,Quan_price,Quan_time,Quan_surplus,Quan_receive,Quan_condition,Quan_m_link,Quan_link,Ctime';
        $res = DtkModel::where('ID','>','1')->selectRaw($select)->paginate(20);
        //$res->links();
        //dd($res);
        if($res)
        {
            return $res;
        }
        return false;
    }
    public function getHomeListData()
    {//为首页获取产品
        $sql = "select A.ID,A.GoodsID,A.Title,A.D_title,A.Pic,A.Cid,A.Org_Price,A.Price,A.IsTmall,A.Sales_num,A.Dsr,A.SellerID,A.Yongjin_type,A.Commission,A.Commission_jihua,A.Commission_queqiao,A.Jihua_link,A.Que_siteid,A.Jihua_shenhe,A.Introduce,A.Quan_id,A.Quan_price,A.Quan_time,A.Quan_surplus,A.Quan_receive,A.Quan_condition,A.Quan_m_link,A.Quan_link,A.Ctime,
                        B.taokeShortUrl,B.quanShortUrl,B.quanUrl,B.taokeUrl,B.taoKouling,B.quanKouling,B.quanPrice,B.fansbuyUrl,B.fansbuyShortUrl 
from think_dataoke_product as A left join think_promoter_dataoke_product as B on A.GoodsID=B.goodsId limit 10";
        $res = DB::select($sql);
        if($res)
        {
            return $res;
        }
        return false;
    }
    public function getInfo($id)
    {

    }

    public function queryData($sql)
    {//执行sql语句

    }
    public function insertDataToSql($data)
    {//不进行入库只进行返回sql语句操作
        //Yongjin_typ之后接口多的字段
        $sql = "INSERT INTO think_dataoke_product (`ID`, `GoodsID`,`Title`,`D_title`,`Pic`,`Cid`,`Org_Price`,`Price`,`IsTmall`,`Sales_num`,`Dsr`,`SellerID`,`Yongjin_type`,`Commission`,`Commission_jihua`,`Commission_queqiao`,`Jihua_link`,`Que_siteid`,`Jihua_shenhe`,`Introduce`,`Quan_id`,`Quan_price`,`Quan_time`,`Quan_surplus`,`Quan_receive`,`Quan_condition`,`Quan_m_link`,`Quan_link`)
VALUES ('".$data['ID']."','".$data['GoodsID']."','".$data['Title']."','".$data['D_title']."','".$data['Pic']."','".$data['Cid']."','".$data['Org_Price']."','".$data['IsTmall']."','".$data['Sales_num']."','".$data['Dsr']."','".$data['SellerID']."','".$data['Yongjin_type']."','".$data['Commission']."','".$data['Commission_jihua']."','".$data['Commission_queqiao']."','".$data['Jihua_link']."','".$data['Que_siteid']."','".$data['Jihua_shenhe']."','".$data['Introduce']."','".$data['Quan_id']."','".$data['Quan_price']."','".$data['Quan_time']."','".$data['Quan_surplus']."','".$data['Quan_surplus']."','".$data['Quan_receive']."','".$data['Quan_condition']."','".$data['Quan_m_link']."','".$data['Quan_link']."')";
        return $sql;
    }

    public function updateDataToSql($data)
    {//不进行入库只进行返回sql语句操作
        $sql = "UPDATE think_dataoke_product SET `ID` = '".$data['ID']."',`GoodsID`='".$data['GoodsID']."',`Title`='".$data['Title']."',`D_title`='".$data['D_title']."',`Pic`='".$data['Pic']."',`Cid`='".$data['Cid']."',`Org_Price`='".$data['Org_Price']."',`Price`='".$data['Price']."',`IsTmall`='".$data['IsTmall ']."',`Sales_num`='".$data['Sales_num']."',`Dsr`='".$data['Dsr']."',`SellerID`='".$data['SellerID']."',`Yongjin_type`='".$data['Yongjin_type']."',`Commission`='".$data['Commission']."',`Commission_jihua`='".$data['Commission_jihua']."',`Commission_queqiao`='".$data['Commission_queqiao']."',`Jihua_link`='".$data['Jihua_link']."',`Que_siteid`='".$data['Que_siteid']."',`Jihua_shenhe`='".$data['Jihua_shenhe']."',`Introduce`='".$data['Introduce']."',`Quan_id`='".$data['Quan_id']."',`Quan_price`='".$data['Quan_price']."',`Quan_time`='".$data['Quan_time']."',`Quan_surplus`='".$data['Quan_surplus']."',`Quan_receive`='".$data['Quan_receive']."',`Quan_condition`='".$data['Quan_condition']."',`Quan_m_link`='".$data['Quan_m_link']."',`Quan_link`='".$data['Quan_link']."' WHERE `ID` = '".$data['ID']."' ";
        return $sql;
    }
    public function isExist($id)
    {
        if($id=='') exit('id出问题了');
        $res = self::find($id);
        //$res = self::where(['ID'=>$id])->get();
        if($res==null)
        {
            return false;
        }
        else{
            return true;
        }

    }
    public function delData($day=2)
    {//删除操作，删除2天前的数据

        $now = time()-(60*60*24*$day);
        $times=date("Y-m-d H:i:s",$now);


        //$where = [['id','>',1],['Ctime','<',"$times"]];
        //$res = self::whereColumn($where)->delete();
        //$res = self::where("id",">","1")->where("Ctime","<",$times)->limit('50')->delete();
        $res = self::where("id",">","1")->where("Ctime","<",$times)->delete();

        if($res)
        {
            return false;
        }
        else{
            return $res;
        }
    }
    public function getImgById($goodsId)
    {//通过goodsId获取图片
        $sql = "select Pic from think_dataoke_product where GoodsID=?";
        $data = [$goodsId];
        $res = DB::select($sql,$data);
        if($res)
        {
            return $res;
        }
        return false;
    }






}
